﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using DownloadInterface;

namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
  
   public partial class MainWindow : Window,IUserInterface
   {

       MyList taskList = new MyList();
       ObservableCollection<FileCategory> MyCategoryList = new ObservableCollection<FileCategory>();
       DownloadTask newTask;
       DownloadTask CurrentSelected;
       Dictionary<TaskStatus, string> imgSourceDict = new Dictionary<TaskStatus, string>();

       private FileManage fileManager;
       private DownloadManager downloadManager;

       public MainWindow()
       {
           InitializeComponent();
           newTask=  new DownloadTask(null, null, null, null);
           CurrentSelected= new DownloadTask(null, null, null, null);
           //taskList.Add(new DownloadTask("sw","wewe","ewew",null));
          // isplayDownloadTaskList(taskList);
           //FileCategory f = new FileCategory { categoryName = "第一" };
           //MyCategoryList.Add(f);
           //displayDownloadedFileCategory(MyCategoryList);
          

           imgSourceDict.Add(TaskStatus.STATUS_STOPPED, "/WpfApplication2;component/Images/开始button");
           imgSourceDict.Add(TaskStatus.STATUS_DOWNLOADING, "/WpfApplication2;component/Images/暂停button");

           fileManager = Serializer.DeSerializeFileManager("fm.bin");
           downloadManager = Serializer.DeSerializeDownloadManager("dm.bin");
           //FileManage fileManager = new FileManage();
           
           DownloadController downloadController = new DownloadController(downloadManager,fileManager,this);
       }

        private void ListViewItem_Selected(object sender, RoutedEventArgs e)
        {

        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         CurrentSelected = (DownloadTask)listView1.SelectedItem;
         Uri uri;
            BitmapImage bimg ;
            if (CurrentSelected == null)
            {
                return;
            }
            else
            {
                uri = new Uri("/WpfApplication2;component/Images/删除.png", UriKind.Relative);
                bimg = new BitmapImage(uri);
                Delete.Source = bimg;
            }
             uri = new Uri(imgSourceDict[CurrentSelected.taskStatus.taskStatus] + ".png", UriKind.Relative);
             bimg = new BitmapImage(uri);
             Start.Source = bimg;
        }

        private void ListViewItem_Selected_1(object sender, RoutedEventArgs e)
        {

        }
        
        private void image1_MouseEnter(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
                return;
            CurrentSelected = (DownloadTask)listView1.SelectedItem;
   
               Uri uri = new Uri(imgSourceDict[CurrentSelected.taskStatus.taskStatus]+"2.png", UriKind.Relative);
               BitmapImage bimg = new BitmapImage(uri);
                Start.Source = bimg;
      
        }

        private void image1_MouseLeave(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }      
               Uri uriStart = new Uri(imgSourceDict[CurrentSelected.taskStatus.taskStatus]+".png", UriKind.Relative);
               BitmapImage bimgStart = new BitmapImage(uriStart);
                Start.Source = bimgStart;
        
        }

        private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (listView1.SelectedItems.Count == 0)
            {
                return;
            }
            Uri uri = new Uri(imgSourceDict[CurrentSelected.taskStatus.taskStatus]+"2.png", UriKind.Relative);
            BitmapImage bimg = new BitmapImage(uri);
            Start.Source = bimg;
        }

        private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (listView1.SelectedItem==null)
            {
                return;
            }
            if (CurrentSelected.taskStatus.taskStatus == TaskStatus.STATUS_DOWNLOADING)
            {
                OperateDownloadTask(DownloadTaskOperation.DOWNLOAD_PAUSE);
                CurrentSelected.taskStatus.taskStatus = TaskStatus.STATUS_STOPPED;
            }
            else
            {
                OperateDownloadTask(DownloadTaskOperation.DOWNLOAD_CONTINUE);
                CurrentSelected.taskStatus.taskStatus = TaskStatus.STATUS_DOWNLOADING;
            }
            Uri uri = new Uri(imgSourceDict[CurrentSelected.taskStatus.taskStatus]+".png", UriKind.Relative);
            BitmapImage bimg = new BitmapImage(uri);
            Start.Source = bimg;
           
        }


        private void New_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            newTask = new DownloadTask(null, null, "Download", null);
            DialogWindow w = new DialogWindow(newTask);
         
            w.ShowDialog();
            

            if (newTask.fileName == null||newTask.downloadUrl ==null)
            {
                return;
            }

            OperateDownloadTask(DownloadTaskOperation.DOWNLOAD_NEW);
        }

        private void Delete_MouseEnter(object sender, MouseEventArgs e)
        {
            if (listView1.SelectedItem == null)
                return;
            Uri uri = new Uri("/WpfApplication2;component/Images/删除button2.png", UriKind.Relative);
            BitmapImage bimg = new BitmapImage(uri);
            Delete.Source = bimg;
        }

        private void Delete_MouseLeave(object sender, MouseEventArgs e)
        {
            Uri uri;
            BitmapImage bimg;
            if (listView1.SelectedItem == null)
            {
                uri = new Uri("/WpfApplication2;component/Images/删除button_dark1.png", UriKind.Relative);
            }
            else uri = new Uri("/WpfApplication2;component/Images/删除.png", UriKind.Relative);
            bimg = new BitmapImage(uri);
            Delete.Source = bimg;
        }

        private void Delete_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (listView1.SelectedItem == null)
            {
                return;
            }
            OperateDownloadTask(DownloadTaskOperation.DOWNLOAD_CANCEL);
        }

        private void Category_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CategoryChanged();
        }



       // implement download interface


        public event operateDownloadedFile OperateDownloadFile;
        public event operateDowloadTask OperateDownloadTask;
        public event CategoryChangedEventHandler CategoryChanged;
        public event DownloadCompletedEventHandler DownloadCompleted;

        public void displayDownloadTaskList(IEnumerable<DownloadTask> downloadTaskList)
        {
            listView1.ItemsSource = downloadTaskList;
            this.DataContext = downloadTaskList;
        }

        public void displayDownloadedFileCategory(IEnumerable<FileCategory> categoryList)
        {
           Category.ItemsSource = categoryList;
           Category.DataContext = categoryList;
        }

        public void displayDownloadedFileList(IEnumerable<DownloadedFile> downloadedFileList)
        {
            CategoryList.ItemsSource = downloadedFileList;
        }

        public FileCategory getCurrentCategory()
        {
           return  (FileCategory)Category.SelectedItem;
        }
        public DownloadedFile getCurrentFile()
        {
            return (DownloadedFile)CategoryList.SelectedItem;
        }
        public DownloadTask getCurrentTask()
        {
            return (DownloadTask)listView1.SelectedItem;
        }
        public DownloadTask getNewDownloadTask()
        {
            return newTask;
        }
        
       
        public void handleDownloadCompleted(DownloadTask task)
        {
            this.Dispatcher.Invoke(new DownloadCompletedEventHandler((downloadTask) => { DownloadCompleted(downloadTask); }), task); 
        }

        
        private void Window_Closed(object sender, EventArgs e)
        {
            WindowClose();
            Serializer.SerializeFileManager("fm.bin",fileManager);
            Serializer.SerializeDowloadManager("dm.bin", downloadManager);
        }





        public event WindowCloseEventHandler WindowClose;

        public void displayDownloadError(string errorDiscription)
        {
            ErrorMessage Error = new ErrorMessage(errorDiscription);
            Error.ShowDialog();
        }

        private void CategoryList_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
        {
       
        }

        private void Deletefile_Click(object sender, RoutedEventArgs e)
        {
            if (CategoryList.SelectedItem == null)
                return;
            else
            {
                OperateDownloadFile(DownloadedFileOperation.FILE_DELETE);
            }
        }

        private void New_MouseEnter(object sender, MouseEventArgs e)
        {
            Uri uri;
            BitmapImage bimg;
            uri = new Uri("/WpfApplication2;component/Images/新建button2.png", UriKind.Relative);
            bimg = new BitmapImage(uri);
            New.Source = bimg;
        }

        private void New_MouseLeave(object sender, MouseEventArgs e)
        {
            Uri uri;
            BitmapImage bimg;
            uri = new Uri("/WpfApplication2;component/Images/新建button.png", UriKind.Relative);
            bimg = new BitmapImage(uri);
            New.Source = bimg;
        }
   }

     public class MyList : ObservableCollection<DownloadTask>
     {
         public MyList()
         {

         }
    }
  
}
